<template>
  <div>
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>确认要下发盘点编号为（{{ formItem.code }}）的盘点计划吗？该盘点计划下包含（{{ formItem.dutyItems.length }}）条盘点任务</span>
      </div>
      <div v-for="(o,index) in formItem.dutyItems" :key="index" class="text item">
        <el-divider />
        <el-form>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="任务名称:">
                <span>{{ o.name }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="任务编码:">
                <span>{{ o.code }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="任务责任人:">
                <span>{{ o.userName }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col v-if="o.locations.length > 0" :span="24">
              <el-form-item label="盘点位置:">
                <span v-for="(m,i) in o.locations" :key="i"><el-tag>{{ m.name }}</el-tag></span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col v-if="o.orgRelation.length > 0" :span="24">
              <el-form-item label="盘点部门:">
                <span v-for="(m,i) in o.orgRelation" :key="i"><el-tag>{{ m.name }}</el-tag></span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col v-if="o.assetTypes.length > 0" :span="24">
              <el-form-item label="盘点类型:">
                <span v-for="(m,i) in o.assetTypes" :key="i"><el-tag>{{ m.name }}</el-tag></span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  detail,
  issue
} from '@neu/dcam-ui/api/inventoryPlan'
export default {
  name: 'InventoryPlanConfirmForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      formItem: {
        name: '',
        code: '',
        dutyItems: []
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      }
    },
    /** 盘点下发确认 */
    onOk() {
      if (this.formItem.dutyItems.length <= 0) {
        this.$message({
          showClose: true,
          message: '计划中不含有任务,不能下发计划！',
          type: 'warning'
        })
        return false
      }
      return new Promise(resolve => {
        resolve(issue(this.id))
      })
    }
  }
}
</script>
